package com.example.dayu521.msg_receive.service.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

//https://docs.spring.io/spring-amqp/docs/current/reference/html/#async-annotation-driven-enable
@RabbitListener
@Slf4j
@Component
public class MesgReceive {

    //https://docs.spring.io/spring-amqp/docs/current/reference/html/#Jackson2JsonMessageConverter-from-message
    //https://docs.spring.io/spring-amqp/docs/current/reference/html/#annotation-method-selection
    //确认模式是auto,方法正常返回后就会确认
    @RabbitHandler
    public void recv_seckill_shangjia(@Payload Map<String,Long> ax){
        ax.forEach((i,j)->{
            log.info("{}-{}",i,j);
        });

    }

    //手动确认
    @RabbitListener(ackMode = "MANUAL")
    public void manual_ack(Channel channel,@Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
//        channel.basicPublish();
        channel.basicAck(tag,true);
    }
}
